﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL.Managers;
using BLL.Entities;
using BLL;
using MessageBoxUtils;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for MalfunctionsListWindow.xaml
    /// </summary>
    public partial class MalfunctionsListWindow : Window
    {
        public MalfunctionsListWindow()
        {
            InitializeComponent();
            loadAllMlfunctions();
        }

        private void loadAllMlfunctions()
        {
            int status = filterComboBox.SelectedIndex;
            malfunctionsListBox.DisplayMemberPath = "Subject";
            malfunctionsListBox.ItemsSource = MalfunctionManager.Instance.GetAllMalfunctions(status);
        }

        private void addMalfunctionButton_Click(object sender, RoutedEventArgs e)
        {

            AddMalfunctionWindow window = new AddMalfunctionWindow();
            window.ShowDialog();
            loadAllMlfunctions();
        }

        private void removeMalfunctionButton_Click(object sender, RoutedEventArgs e)
        {
            if ((malfunctionsListBox.SelectedItem as Malfunction).CloserId != -1)
            {
                MessageBoxResult result = WPFMessageBox.Show("This Malfunction Is Already Closed", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBoxResult result = WPFMessageBox.Show("Are You Sure You Want To Remove This Malfunction?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    MalfunctionManager.Instance.CloseMalfunction(malfunctionsListBox.SelectedItem as Malfunction);
                }
            }

            loadAllMlfunctions();
        }

        private void displayMalfunctionButton_Click(object sender, RoutedEventArgs e)
        {
            Malfunction selected = malfunctionsListBox.SelectedItem as Malfunction;
            DisplayMalfunctionWindow view = new DisplayMalfunctionWindow(selected);
            view.ShowDialog();
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadAllMlfunctions();
        }

        private void reminderButton_Click(object sender, RoutedEventArgs e)
        {
            List<Malfunction> openMalfunctions = MalfunctionManager.Instance.GetAllMalfunctions(1);
            StringBuilder stringBuilder = new StringBuilder();
            label2.Content = string.Empty;

            foreach (Malfunction malfunction in openMalfunctions)
            {
                stringBuilder.Append("Subject: ").Append(malfunction.Subject).Append("\n");
                stringBuilder.Append("Decription: ").Append(malfunction.Description).Append("\n");
                stringBuilder.Append("Time created: ").Append(malfunction.CreatedTime.ToString()).Append("\n");
                GymWorker creator = GymWorkerManager.Instance.GetWorkerById(malfunction.CreatorId);
                stringBuilder.Append("Created by: ").Append(creator.FullName).Append("\n");
                stringBuilder.Append("\n");
            }

            string[] admins = (from worker in GymWorkerManager.Instance.GetWorkers(true)
                               select worker.Email).ToArray();

            bool res = EmailService.SendEmail("Open Malfunctions Reminder", stringBuilder.ToString(), admins);
            if (res == true)
            {
                label2.Content = "Email Was Sent!";
                label2.Foreground = Brushes.Green;
            }

            else
            {
                label2.Content = "An Error Occurd While Sending Emails!";
                label2.Foreground = Brushes.Red;
            }
        }
    }
}
